package com.tcy.game.angryrobots.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Align;
import com.tcy.game.angryrobots.AngryRobotsGame;
import com.tcy.game.angryrobots.Assets;
import com.tcy.game.angryrobots.general.CameraHelper;
import com.tcy.game.angryrobots.general.CommonUtils;
import com.tcy.game.angryrobots.general.GameScreen;
import com.tcy.game.angryrobots.general.SimpleButton;

import static com.tcy.game.angryrobots.Assets.*;

/**
 * MainMenuScreen
 * Created by 80002023 on 2016/7/27.
 */
public class MainMenuScreen extends GameScreen<AngryRobotsGame> {

    final String TITLE = "ANGRY ROBOTS";
    final String JIBBER_JABBER = "Robots are deadly\nWalls are deadly\nDoors are deadly\nShots are deadly\n\nDestroy robots to get the high score";
    SpriteBatch spriteBatch;
    private OrthographicCamera menuCamera;
    private SimpleButton playButton;
    private SimpleButton scoresButton;
    private Vector3 touchPoint;
    private boolean wasTouched;

    public MainMenuScreen(AngryRobotsGame game) {
        super(game);
    }

    private void createButtons() {
        playButton = createPlayButton();
        playButton.setWidth(width / 4);
        playButton.setHeight(height / 4);
        if (game.canShowScores()) {
            scoresButton = createShowScoresButton();
            scoresButton.setWidth(width / 4);
            scoresButton.setHeight(height / 4);
            scoresButton.rightOn(width);
            scoresButton.bottomOn(1.0f);
            playButton.leftOn(0);
            playButton.bottomOn(1.0f);
        } else {
            playButton.bottomOn(1.0f);
            playButton.centerHorizontallyOn(width / 2);
        }
    }

    private SimpleButton createPlayButton() {

        return new SimpleButton("Play", Assets.textFont);
    }

    private SimpleButton createShowScoresButton() {
        return new SimpleButton("Scores", Assets.textFont);
    }

    @Override
    public void render(float delta) {
        updateButtons(delta);
        if (playButton.wasPressed()) {
            CommonUtils.log("MainMenuScreen", "render play button pressed");
            playButtonSound();
            startGame();
        } else if (game.canShowScores() && scoresButton.wasPressed()) {
            CommonUtils.log("MainMenuScreen", "render game can show scores && play button pressed");
            playButtonSound();
            showScores();
        } else {
            Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
            spriteBatch.begin();
            Assets.scoreFont.setColor(Color.WHITE);
            Assets.scoreFont.getData().setScale(.1f);
            float y = height - Assets.scoreFont.getCapHeight() - 7;
            Assets.scoreFont.draw(spriteBatch, TITLE, 0, y, width, Align.center, false);

            Assets.textFont.getData().setScale(.08f);
            Assets.textFont.draw(spriteBatch, JIBBER_JABBER, width / 8, 3 * height / 4,
                    3 * width / 4, Align.center, false);
            drawButtons();
            spriteBatch.end();
        }
    }

    private void playButtonSound() {
        Sound buttonPressedSound = Assets.buttonSound;
        Assets.playSound(buttonPressedSound);
    }

    private void startGame() {
        game.setScreen(new WorldPresenter(game));
    }

    private void showScores() {
        game.showScores();
    }

    private void updateButtons(float delta) {
        touchPoint = screenToViewport(Gdx.input.getX(), Gdx.input.getY());
        boolean justTouched = Gdx.input.justTouched();
        boolean isTouched = Gdx.input.isTouched();
        boolean justReleased = wasTouched && !isTouched;
        wasTouched = isTouched;
        playButton.update(delta, justTouched, isTouched, justReleased, touchPoint.x, touchPoint.y);
        if (game.canShowScores()) {
            scoresButton.update(delta, justTouched, isTouched, justReleased, touchPoint.x, touchPoint.y);
        }
    }

    private void drawButtons() {
        playButton.draw(spriteBatch);
        if (game.canShowScores()) {
            CommonUtils.log("MainMenuScreen", "drawButtons game can Show Scores");
            scoresButton.draw(spriteBatch);
        }
    }

    private Vector3 screenToViewport(float x, float y) {
        menuCamera.unproject(touchPoint.set(x, y, 0));
        return touchPoint;
    }

    @Override
    public void show() {
        CommonUtils.log("MainMenuScreen", "graphics width: " + width + ", height: " + height);
        menuCamera = CameraHelper.createCamera2(CameraHelper.ViewportMode.PIXEL_PERFECT, width, height, Assets.pixelDensity);
        spriteBatch = new SpriteBatch();
        spriteBatch.setProjectionMatrix(menuCamera.combined);
        touchPoint = new Vector3();
        createButtons();
    }
}
